<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>侦听器</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>

<body>
    <div id="app"></div>
</body>
<script>
    let vm = {
        data () {
            return {
                firstName: 'Galaxy',
                lastName: 'Sa',
                watchFullName: ''
            }
        },
        template: `<div>{{fullName}}{{watchFullName}}</div>`,
        computed: {
            fullName () {
                return this.firstName + this.lastName
            }
        },
        watch: {
            // 当属性lastName发生变化将触发该函数
            lastName (newValue, oldValue) {
                console.log(newValue, oldValue);
                this.watchFullName = this.firstName + this.lastName
            }
        },
        mounted () {
            setTimeout(() => {
                this.lastName = 'Night'
            }, 2000)
        }
    }
    Vue.createApp(vm).mount('#app')
</script>

</html>